<?php
/**
 * User: 1971229701@qq.com
 * Date: 2020/11/6/006
 * Time: 10:27
 */

namespace app\index\logic;


use app\common\model\Cate;
use app\common\model\Consult;
use app\common\model\Lists;

class ListsLogic extends BaseLogic
{
    public function listing($title = '', $price = '', $pailiang = '', $qudong = '')
    {
        $where = array();

        !empty($price) && $where['jiage'] = $price;
        !empty($pailiang) && $where['pailiang'] = $pailiang;
        !empty($qudong) && $where['qudong'] = $qudong;

        if (!empty($title)) { // 要把这个title给到前台
            $CateList = Cate::field('id,name')->where('name', 'like', '%' . $title . '%')->find();
            if (!empty($CateList)) {
                $where['pid'] = $CateList['id'];
            } else {
                $where[]=['title','like',"%".$title."%"];
            }
        }

        // 列表
        $cList = Lists::where($where)->order('sort,id desc')->paginate(12);
        // 优惠资讯
        $aList = Consult::field('id,title,image,create_time')->order('sort,id desc')->limit(6)->select();

        // 车系列表
        $cate_list = Cate::order('id asc')->select();

        // 价格
        $price = get_arr(Lists::$price);
        // 排量
        $pailiang = get_arr(Lists::$pailiang);
        // 驱动
        $qudong = get_arr(Lists::$drive);
        return compact('cList', 'aList', 'cate_list', 'price', 'pailiang', 'qudong');
    }

    public function detail($id)
    {
        // 详情 ManyImagesArr
        $info = Lists::get($id);
        // 轮播图
        $banner = $info->many_images_arr;
        $liangdian = $info->liang_dian_arr;
        // 热门推荐车型
        $list = Lists::where('hot', 1)->limit(9)->select()->append(["cate_name"]);
        return compact('info', 'banner','liangdian', 'list');
    }

    public function search($data)
    {
        $where = [];
        if (!empty($data['title'])) { // 要把这个title给到前台
            $CateList = Cate::field('id,name')->where('name', 'like', '%' . $data['title'] . '%')->find();
            if (!empty($CateList)) {
                unset($data['title']);
                $where['pid'] = $CateList['id'];
            } else {
                $data['title'] = array('like', '%' . $data['title'] . '%');
            }
        }
        $r = Lists::where($data)->where($where)->limit($this->getPagingParams())->select();
        return $r;
    }
}